<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchMap.data" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="searchMap.data.specName"
          placeholder="规格名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="dialogFormVisible_add = true;entity={
         options:[
         ],
      }" 
          >规格添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="delChoose()"
          >批量删除</el-button>
      </el-form-item>
      </el-form>
      <el-table ref="multipleTable" :data="goodSpecList" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column   type="selection" width="80">
      </el-table-column>
      <el-table-column  v-model="entity.id" prop="id" label="规格ID" width="180">
      </el-table-column>
      <el-table-column v-model="entity.specName" prop="specName" label="规格名称" width="180">
      </el-table-column>
      <el-table-column prop="" label="操作" width="180">
        <template slot-scope="scope">
          <div>
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
              >修改</el-button
              >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchMap.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="this.total"
    >
    </el-pagination>
    <!-- 添加品牌 -->
      <el-dialog title="规格编辑" :visible.sync="dialogFormVisible_add">
      <el-form :model="entity">
        <el-form-item label="规格名称" >
          <el-input v-model="entity.specName" autocomplete="off"></el-input>
        </el-form-item>
        <el-button type="success" @click="addRow()"
          >新增规格选项</el-button>
       <el-table :data="entity.options" style="width: 100%">
       <el-table-column prop="date" label="规格选项" >
           <template slot-scope="scope">
               <el-input  placeholder="请输入规格选项" v-model="entity.options[scope.$index].optionName" class="itemName" />
           </template>
      </el-table-column>
      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <div>
            <el-button
              type="primary"
              icon="el-icon-delete"
              @click="delRow(scope.$index,scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
      
      </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveDate()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改品牌 -->
     <el-dialog title="规格修改" :visible.sync="dialogFormVisible_edit">
      <el-form :model="entity">
        <el-form-item label="规格名称" >
          <template >
          <el-input v-model="editDialogData.specName" autocomplete="off"></el-input>
          </template>
        </el-form-item>
        <el-button type="success" @click="addRow()"
          >新增规格选项</el-button>
       <el-table :data="entity.options" style="width: 100%">
       <el-table-column prop="option" label="规格选项" width="180">
               <template slot-scope="scope">
               <el-input v-model="editDialogData.options[scope.$index].optionName" placeholder="请输入规格选项"  class="itemName" />
               </template>
      </el-table-column>
      <el-table-column prop="order" label="排序" width="180">
      </el-table-column>
      
      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <div>
            <el-button
              type="primary"
              icon="el-icon-delete"
              @click="delRow(scope.$index,scope.row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
      
      </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editConfirm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { search, save, del, edit } from "@/api/goodSpec";
export default {
  data() {
    return {
      dialogFormVisible_edit: false, //编辑弹窗开关
      dialogFormVisible_add: false, //添加弹窗开关
      visible: false, //删除弹窗开关
      goodSpecList: [],
      selectionList:[],
      entity:{
         options:[
         ],
      },
      total: 0,
      searchMap: {
        page: 1,
        limit: 5,
        data: {},
      },
      delData: {}, 
      editDialogData: {
        options:[
         ],
      }, 
      gooodSpec: {}, 
    };
  },
  methods: {
    handleSelectionChange(selection){
		    this.selectionList = []
		    selection.forEach(element => {
		        this.selectionList.push(element)
		    });
		},
    delChoose(){
    var that = this;
    console.log(that.selectionList);
    this.delData = that.selectionList;
    del(this.delData).then((res)=>{
      if (res.success) {
          that.search();
          this.$message({
            showClose: true,
            message: res.msg,
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        }
        console.log(res);
    });
    },
    delRow(index,row) {
     this.entity.options.splice(index,1)
    },
    addRow(){
    //console.log(this.entity);
    this.entity.options.push({});
    },
    //搜索
    search() {
      var _this = this;
      search(this.searchMap).then((res) => {
          //console.log(res.data);
          _this.goodSpecList=res.data;
        //_this.entity = res.data;
        _this.total = res.total;
      });
    },
    //保存
    saveDate() {
      //console.log(this.entity);
      save(this.entity).then((res) => {
        console.log(res);
        if (res.success) {
          this.search();
          this.$message({
            showClose: true,
            message: res.msg,
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        }
        this.dialogFormVisible_add = false;
      });
    },
    //删除
    handleDelete(index, row) {
      this.delData = row;
      del(this.delData).then((res) => {
        if (res.success) {
          this.search();
          this.$message({
            showClose: true,
            message: res.msg,
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    
    //编辑
    handleEdit(index, row) {
      let that = this;
      console.log(row);
      that.dialogFormVisible_edit = true;
      that.editDialogData = row;

    },

    editConfirm() {
      console.log(this.editDialogData);
      edit(this.editDialogData).then((res) => {
        if (res.success) {
          this.search();
          this.$message({
            showClose: true,
            message: res.msg,
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        }
        this.dialogFormVisible_edit = false;
      });
    },
    handleSizeChange(size) {
      this.searchMap.limit = size;
      this.search();
    },
    handleCurrentChange(currentPage) {
      this.searchMap.page = currentPage;
      this.search();
    },
  },
  created() {
    let that = this;
    that.search();
  },
};
</script>